/**
 * 导入
 * @requires encryptParam 加密
 */
import axios from 'axios';
import { apiList, } from './config';
import { ErrorType, ResType } from '../types/response';

// 设置参数
const instance = axios.create({
  timeout: 1000,
  baseURL: apiList[0],
});

// 请求之前
instance.interceptors.request.use((config: any) => {
  return config;
}, (error: any) => Promise.reject(error));

/**
 * 请求之后
 * @error 任何超出2xx范围的状态码都会触发此函数（做一些响应错误）
 */
instance.interceptors.response.use((response: any) => {
  return response.data;
}, (error: any) => {
  let data: ErrorType = error?.response?.data;
  let res: ResType = {
    code: 500,
    message: '网络错误',
    data: '',
  };
  if (data?.code !== 200) {
    res.code = data.code;
    res.message = data.message;
    res.data = data.data;
  }
  return res;
});

// 导出
export default instance;